#include "cmdtype.h"

void analyze_cmd(int clientfd)
{
    while (1)
	{
		printf("请输入指令:");
		fflush(stdout);

		char cmd[100] = {0};
		int i = 0;
		char ch = -1;
		while ((ch = getchar()) != '\n')
		{
			cmd[i++] = ch;
		}

		char Cmd[10] = {0};
		int len = 0;
		while (cmd[len] && cmd[len] != ' ')
		{
			len++;
		}
		strncpy(Cmd, cmd, len);
		char para[100] = {0};
		strncpy(para, cmd + len + 1, 100 - len);

		//先发四字节指令类型，再发100字节参数
		if (strcmp(Cmd, "ls") == 0)
		{
			int cmd = CMD_TYPE_LS;
			send(clientfd, &cmd, sizeof(cmd), MSG_WAITALL);
		}
		else if (strcmp(Cmd, "cd") == 0)
		{
			int cmd = CMD_TYPE_CD;
			send(clientfd, &cmd, sizeof(cmd), MSG_WAITALL);
		}
		else if (strcmp(Cmd, "pwd") == 0)
		{
			int cmd = CMD_TYPE_PWD;
			send(clientfd, &cmd, sizeof(cmd), MSG_WAITALL);
		}
		else if (strcmp(Cmd, "puts") == 0)
		{
			//上传下载文件需要执行对应函数进行
			int cmd = CMD_TYPE_PUT;
			send(clientfd, &cmd, sizeof(cmd), MSG_WAITALL);
			int len = putsFile(clientfd, para);
			continue;
		}
		else if (strcmp(Cmd, "gets") == 0)
		{
			int cmd = CMD_TYPE_GET;
			send(clientfd, &cmd, sizeof(cmd), MSG_WAITALL);
			getsFile(clientfd, para);
			continue;
		}
		else if (strcmp(Cmd, "rm") == 0)
		{
			int cmd = CMD_TYPE_RM;
			send(clientfd, &cmd, sizeof(cmd), MSG_WAITALL);
		}
		else if (strcmp(Cmd, "mkdir") == 0)
		{
			int cmd = CMD_TYPE_MKDIR;
			send(clientfd, &cmd, sizeof(cmd), MSG_WAITALL);
		}
		else
		{
			printf("error cmd\n");
			continue;
		}
		sendMsg(clientfd,para);

		//展示结果
		char recvbuf[4096];
		memset(&recvbuf, 0, 4096);
		printf("开始接收结果\n");
		int recvlen = 0;
		recvCircle(clientfd, (char *)&recvlen, 4);
		printf("recvlen = %d\n",recvlen);
		recvCircle(clientfd, recvbuf, recvlen);
		puts(recvbuf);
	}
}

